package com.example.mybatis.exception;

/**
 * Created with IntelliJ IDEA.
 * User: ThinkPad
 * Date: 2021-04-25 20:14
 * This is my work in reachauto code.
 * mail:chenxiangning@reachauto.com
 * Description:
 */
import com.example.mybatis.vo.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    private static final String DEFAULT_ERROR_VIEW = "error";

    /**
     * 统一 json 异常处理
     *
     * @param exception JsonException
     * @return 统一返回 json 格式
     */
    @ExceptionHandler(value = JsonException.class)
    @ResponseBody
    public ApiResponse jsonErrorHandler(JsonException exception) {
        log.error("JsonException:{}", exception.getMessage());
        return ApiResponse.ofException(exception);
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ApiResponse jsonErrorHandler(Exception exception) {
        log.error("BaseException:{}", exception.getMessage());

        return ApiResponse.ofException(new BaseException(500, exception.getMessage()));
    }

}
